package com.lemon.smartsafety.rayeye;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.entity.tab.TabEntity;
import com.lemon.smartsafety.rayeye.entity.BundleKey;
import com.lemon.smartsafetynew.ui.fragment.base.BaseFragment;
import com.lemon.smartsafety.ui.widget.ViewPagerEnableScroll;
import com.lemon.smartsafety.utils.common.LoggerHelper;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Cooliean on 16/4/16.
 */
public class FireFragment extends BaseFragment {
    private LoggerHelper loggerHelper = new LoggerHelper(FireFragment.class);

    private String[]                   mTitles      = {"未处理", "已确认", "已忽略"};
    //0 未处理，1 已确认，2 已忽略
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Bind(R.id.tl_com)
    CommonTabLayout       mTabs;
    @Bind(R.id.viewPager)
    ViewPagerEnableScroll viewPager;

    FragmentStatePagerAdapter mAdapter;

    private int mBuildingId = 0;
    private String mCompanyCode = "";

    public static FireFragment newInstance(String company_code, int buildingId) {
        Bundle bundle = new Bundle();
        bundle.putInt(BundleKey.BUILDING_ID, buildingId);
        bundle.putString(BundleKey.COMPANY_CODE, company_code);
        FireFragment fragment = new FireFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected void getBaseArguments() {
        if (getArguments() != null) {
            mBuildingId = getArguments().getInt(BundleKey.BUILDING_ID, 0);
            mCompanyCode = getArguments().getString(BundleKey.COMPANY_CODE, "");
        }
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_fire_all_detail_list;
    }

    @Override
    protected void initViews() {
        initPageView();
    }


    private void initPageView() {
        mTabEntities.clear();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        mTabs.setTabData(mTabEntities);
        mTabs.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        mAdapter = new FragmentStatePagerAdapter(getFragmentManager()) {

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public Fragment getItem(int position) {
                return FireListFragment.newInstance(mCompanyCode,mBuildingId, position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }
        };
        viewPager.setAdapter(mAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabs.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(0);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
